package com.example.springbootvuetest.controller;

import com.example.springbootvuetest.base.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootvuetest.base.Result;
import lombok.RequiredArgsConstructor;
import com.example.springbootvuetest.entity.Message;
import com.example.springbootvuetest.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * <p>
 * controller
 * </p>
 *
 * @author shuoye
 * @since 2023-03-14
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "REST - ")
@RestController
@RequestMapping("/message")
public class MessageController {
    private final IMessageService service;

    @PostMapping("/send")
    @ApiOperation(value = "发送留言")
    public Result send(@RequestBody Message message) {
        message.setCreateTime(new Date());
        if (service.save(message)) {
            return Result.success(message);
        }
        return Result.error(ResultCode.SYS_ERROR);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public Result delete(@PathVariable("id") Integer id) {
        boolean flag = service.removeById(id);
        return flag ? Result.success() : Result.error();
    }

    @GetMapping("/idle")
    @ApiOperation(value = "获取闲置物品的留言")
    public Result getAllIdleMessage(@RequestParam("idleId") Integer idleId) {
        return Result.success(service.getAllIdleMessage(idleId));
    }

    @GetMapping("/my")
    @ApiOperation(value = "获取所有关于我的留言")
    public Result<List<Message>> getAllMessageItem(@RequestParam("userId") Integer userId) {
        List<Message> list = service.getAllMyMessage(userId);
        return Result.success(list);
    }
}